package com.bruce.learnrecycler.kotlin.ui.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.bruce.learnrecycler.kotlin.action.ActionDelegate
import com.bruce.learnrecycler.kotlin.ui.adapter.viewholder.BaseViewHolder
import com.bruce.learnrecycler.kotlin.ui.adapter.viewholder.TimerViewHolder
import com.bruce.learnrecycler.kotlin.ui.listitem.Item
import com.bruce.learnrecylerview.app.R
import javax.inject.Inject

/**
 * Created by wangzhijie@wind-mobi.com on 2015/9/2.
 */
public class MainAdapter Inject constructor(val inflator: LayoutInflater, val delegate: ActionDelegate) : RecyclerView.Adapter<BaseViewHolder>() {
    public var items: List<Item>? = null;

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): BaseViewHolder? {
        val view = inflator.inflate(viewType, parent, false)
        return if (viewType == R.layout.timer_item) {
            TimerViewHolder(view)
        } else {
            BaseViewHolder(view)
        }
    }

    override fun getItemCount(): Int = items?.size() ?: 0

    override fun onBindViewHolder(holder: BaseViewHolder?, position: Int) {
        holder?.tvMain?.setText(items?.get(position)?.name ?: "null name")
        if (holder is TimerViewHolder) {
            holder.tvTimer.setText(items?.get(position)?.time ?: "null time")
        }
    }

    override fun getItemViewType(position: Int): Int = items?.get(position)?.type ?: 0

}
